local skel = fk.CreateSkill {
  name = "rmt__xichen",
}

Fk:loadTranslationTable{
  ["rmt__xichen"] = "洗尘",
  [":rmt__xichen"] = "每轮限一次，回合结束时，你可以展示任意张因次数或距离限制而无法使用的手牌，然后获得一个仅能使用这些牌且无次数和距离限制的回合。你杀死角色后，重置此技能。",
  ["#rmt__xichen-cost"] = "洗尘：可展示任意张因限制无法使用的手牌，获得一个仅能使用这些牌的额外回合",

  ["$rmt__xichen1"] = "酌我清尊，洗公孤愤，来同一醉。",
  ["$rmt__xichen2"] = "时易失，心徒壮，岁将零。",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  times = function (_, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryRound)
  end,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player:usedSkillTimes(skel.name, Player.HistoryRound) == 0
      and not player:isKongcheng() then
      local room = player.room
      local cards = table.filter(player:getCardIds("h"), function (id)
        return not player:canUse(Fk:getCardById(id))
      end)
      room:addPlayerMark(player, MarkEnum.BypassDistancesLimit)
      room:addPlayerMark(player, MarkEnum.BypassTimesLimit)
      -- 请勿使用extar_data参数
      cards = table.filter(cards, function (id) return player:canUse(Fk:getCardById(id)) end)
      room:removePlayerMark(player, MarkEnum.BypassDistancesLimit)
      room:removePlayerMark(player, MarkEnum.BypassTimesLimit)
      -- 次数限制为“每阶段”的判断
      for _, id in ipairs(player:getTableMark("rmt__xichen_playphase-turn")) do
        if table.contains(player:getCardIds("h"), id) then
          table.insertIfNeed(cards, id)
        end
      end
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local ex_cards = event:getCostData(self).cards
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = #ex_cards, include_equip = false, cancelable = true, skill_name = skel.name,
      pattern = tostring(Exppattern{ id = ex_cards }), prompt = "#rmt__xichen-cost",
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    player:showCards(cards)
    if player.dead then return end
    player:gainAnExtraTurn(true, skel.name, nil, { rmt__xichen_cards = cards })
  end,
})

-- 额外回合开始时，标记本回合可以使用的牌
skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data and data.extra_data and data.extra_data.rmt__xichen_cards
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__xichen_cards-turn", data.extra_data.rmt__xichen_cards)
  end,
})

-- 出牌阶段结束时，监测有没有次数用完的牌，让这些牌额外被“洗尘”回合结束时的效果监测到
skel:addEffect(fk.EventPhaseEnd, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player.phase == Player.Play and not player:isKongcheng() and not player.dead
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local status_skills = Fk:currentRoom().status_skills[TargetModSkill] or Util.DummyTable---@type TargetModSkill[]
    --- 是否无次数限制
    local bypassTimesCheck = function (card)
      for _, skill in ipairs(status_skills) do
        if skill:bypassTimesCheck(player, card.skill, Player.HistoryPhase, card, nil) then
          return true
        end
      end
      return false
    end
    local mark = {}
    for _, id in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(id)
      local maxTimes = card.skill:getMaxUseTime(player, Player.HistoryPhase, card, nil)
      if maxTimes then -- 只有有次数限制的牌才需要监测
        if player:usedCardTimes(card.trueName, Player.HistoryPhase) >= maxTimes
          and not bypassTimesCheck(card) then
          table.insert(mark, id)
        end
      end
    end
    if #mark == 0 then return end
    room:setPlayerMark(player, "rmt__xichen_playphase-turn", mark)
  end,
})

-- 杀死角色后，重置此技能
skel:addEffect(fk.BuryVictim, {
  can_refresh = function (self, event, target, player, data)
    return data.killer == player and player:usedSkillTimes(skel.name, Player.HistoryRound) > 0
      and player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player:setSkillUseHistory(skel.name, 0, Player.HistoryRound)
  end,
})

--- 额外回合中仅能使用指定牌
skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    local mark = player:getMark("rmt__xichen_cards-turn")
    if mark ~= 0 then
      local cards = Card:getIdList(card)
      return #cards == 0 or table.find(cards, function (id) return not table.contains(mark, id) end)
    end
  end,
})

-- 额外回合中用牌无次数距离限制
skel:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and player:getMark("rmt__xichen_cards-turn") ~= 0
  end,
  bypass_distances = function(self, player, skill, card, to)
    return player:getMark("rmt__xichen_cards-turn") ~= 0
  end,
})

return skel
